summaryrefslogtreecommitdiffstats
path: root/src/input_common/helpers/joycon_protocol/calibration.cpp
blob: cd30ab8698c0122564f00c1b997c3b6215bb9ea2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <cstring>

#include "input_common/helpers/joycon_protocol/calibration.h"
#include "input_common/helpers/joycon_protocol/joycon_types.h"

namespace InputCommon::Joycon {

CalibrationProtocol::CalibrationProtocol(std::shared_ptr<JoyconHandle> handle)
    : JoyconCommonProtocol(std::move(handle)) {}

DriverResult CalibrationProtocol::GetLeftJoyStickCalibration(JoyStickCalibration& calibration) {
    std::vector<u8> buffer;
    DriverResult result{DriverResult::Success};
    calibration = {};
    SetBlocking();

    result = ReadSPI(CalAddr::USER_LEFT_MAGIC, sizeof(u16), buffer);

    if (result == DriverResult::Success) {
        const bool has_user_calibration = buffer[0] == 0xB2 && buffer[1] == 0xA1;
        if (has_user_calibration) {
            result = ReadSPI(CalAddr::USER_LEFT_DATA, 9, buffer);
        } else {
            result = ReadSPI(CalAddr::FACT_LEFT_DATA, 9, buffer);
        }
    }

    if (result == DriverResult::Success) {
        calibration.x.max = static_cast<u16>(((buffer[1] & 0x0F) << 8) | buffer[0]);
        calibration.y.max = static_cast<u16>((buffer[2] << 4) | (buffer[1] >> 4));
        calibration.x.center = static_cast<u16>(((buffer[4] & 0x0F) << 8) | buffer[3]);
        calibration.y.center = static_cast<u16>((buffer[5] << 4) | (buffer[4] >> 4));
        calibration.x.min = static_cast<u16>(((buffer[7] & 0x0F) << 8) | buffer[6]);
        calibration.y.min = static_cast<u16>((buffer[8] << 4) | (buffer[7] >> 4));
    }

    // Nintendo fix for drifting stick
    // result = ReadSPI(0x60, 0x86 ,buffer, 16);
    // calibration.deadzone = (u16)((buffer[4] << 8) & 0xF00 | buffer[3]);

    // Set a valid default calibration if data is missing
    ValidateCalibration(calibration);

    SetNonBlocking();
    return result;
}

DriverResult CalibrationProtocol::GetRightJoyStickCalibration(JoyStickCalibration& calibration) {
    std::vector<u8> buffer;
    DriverResult result{DriverResult::Success};
    calibration = {};
    SetBlocking();

    result = ReadSPI(CalAddr::USER_RIGHT_MAGIC, sizeof(u16), buffer);

    if (result == DriverResult::Success) {
        const bool has_user_calibration = buffer[0] == 0xB2 && buffer[1] == 0xA1;
        if (has_user_calibration) {
            result = ReadSPI(CalAddr::USER_RIGHT_DATA, 9, buffer);
        } else {
            result = ReadSPI(CalAddr::FACT_RIGHT_DATA, 9, buffer);
        }
    }

    if (result == DriverResult::Success) {
        calibration.x.center = static_cast<u16>(((buffer[1] & 0x0F) << 8) | buffer[0]);
        calibration.y.center = static_cast<u16>((buffer[2] << 4) | (buffer[1] >> 4));
        calibration.x.min = static_cast<u16>(((buffer[4] & 0x0F) << 8) | buffer[3]);
        calibration.y.min = static_cast<u16>((buffer[5] << 4) | (buffer[4] >> 4));
        calibration.x.max = static_cast<u16>(((buffer[7] & 0x0F) << 8) | buffer[6]);
        calibration.y.max = static_cast<u16>((buffer[8] << 4) | (buffer[7] >> 4));
    }

    // Nintendo fix for drifting stick
    // buffer = ReadSPI(0x60, 0x98 , 16);
    // joystick.deadzone = (u16)((buffer[4] << 8) & 0xF00 | buffer[3]);

    // Set a valid default calibration if data is missing
    ValidateCalibration(calibration);

    SetNonBlocking();
    return result;
}

DriverResult CalibrationProtocol::GetImuCalibration(MotionCalibration& calibration) {
    std::vector<u8> buffer;
    DriverResult result{DriverResult::Success};
    calibration = {};
    SetBlocking();

    result = ReadSPI(CalAddr::USER_IMU_MAGIC, sizeof(u16), buffer);

    if (result == DriverResult::Success) {
        const bool has_user_calibration = buffer[0] == 0xB2 && buffer[1] == 0xA1;
        if (has_user_calibration) {
            result = ReadSPI(CalAddr::USER_IMU_DATA, sizeof(IMUCalibration), buffer);
        } else {
            result = ReadSPI(CalAddr::FACT_IMU_DATA, sizeof(IMUCalibration), buffer);
        }
    }

    if (result == DriverResult::Success) {
        IMUCalibration device_calibration{};
        memcpy(&device_calibration, buffer.data(), sizeof(IMUCalibration));
        calibration.accelerometer[0].offset = device_calibration.accelerometer_offset[0];
        calibration.accelerometer[1].offset = device_calibration.accelerometer_offset[1];
        calibration.accelerometer[2].offset = device_calibration.accelerometer_offset[2];

        calibration.accelerometer[0].scale = device_calibration.accelerometer_scale[0];
        calibration.accelerometer[1].scale = device_calibration.accelerometer_scale[1];
        calibration.accelerometer[2].scale = device_calibration.accelerometer_scale[2];

        calibration.gyro[0].offset = device_calibration.gyroscope_offset[0];
        calibration.gyro[1].offset = device_calibration.gyroscope_offset[1];
        calibration.gyro[2].offset = device_calibration.gyroscope_offset[2];

        calibration.gyro[0].scale = device_calibration.gyroscope_scale[0];
        calibration.gyro[1].scale = device_calibration.gyroscope_scale[1];
        calibration.gyro[2].scale = device_calibration.gyroscope_scale[2];
    }

    ValidateCalibration(calibration);

    SetNonBlocking();
    return result;
}

DriverResult CalibrationProtocol::GetRingCalibration(RingCalibration& calibration,
                                                     s16 current_value) {
    // TODO: Get default calibration form ring itself
    if (ring_data_max == 0 && ring_data_min == 0) {
        ring_data_max = current_value + 800;
        ring_data_min = current_value - 800;
        ring_data_default = current_value;
    }
    ring_data_max = std::max(ring_data_max, current_value);
    ring_data_min = std::min(ring_data_min, current_value);
    calibration = {
        .default_value = ring_data_default,
        .max_value = ring_data_max,
        .min_value = ring_data_min,
    };
    return DriverResult::Success;
}

void CalibrationProtocol::ValidateCalibration(JoyStickCalibration& calibration) {
    constexpr u16 DefaultStickCenter{2048};
    constexpr u16 DefaultStickRange{1740};

    if (calibration.x.center == 0xFFF || calibration.x.center == 0) {
        calibration.x.center = DefaultStickCenter;
    }
    if (calibration.x.max == 0xFFF || calibration.x.max == 0) {
        calibration.x.max = DefaultStickRange;
    }
    if (calibration.x.min == 0xFFF || calibration.x.min == 0) {
        calibration.x.min = DefaultStickRange;
    }

    if (calibration.y.center == 0xFFF || calibration.y.center == 0) {
        calibration.y.center = DefaultStickCenter;
    }
    if (calibration.y.max == 0xFFF || calibration.y.max == 0) {
        calibration.y.max = DefaultStickRange;
    }
    if (calibration.y.min == 0xFFF || calibration.y.min == 0) {
        calibration.y.min = DefaultStickRange;
    }
}

void CalibrationProtocol::ValidateCalibration(MotionCalibration& calibration) {
    for (auto& sensor : calibration.accelerometer) {
        if (sensor.scale == 0) {
            sensor.scale = 0x4000;
        }
    }
    for (auto& sensor : calibration.gyro) {
        if (sensor.scale == 0) {
            sensor.scale = 0x3be7;
        }
    }
}

} // namespace InputCommon::Joycon